<script lang="ts" setup>
import type { Dict } from '@croco/framework/types';

import { onBeforeMount, ref } from 'vue';

import { Page } from '@croco/framework/libraries';

import { ElMessageBox } from 'element-plus';

import { getDictTrim } from '#/api';
import { service } from '#/api/request';
import { DataTable } from '#/components/data-table';

import { useTable } from './data';
import Edit from './edit.vue';

defineOptions({ name: 'SystemUser' });

const dataTableRef = ref<InstanceType<typeof DataTable>>();
const editRef = ref<InstanceType<typeof Edit>>();

const enums = reactive<{
    sex: Array<Dict> | undefined;
    status: Array<Dict> | undefined;
}>({
    sex: undefined,
    status: undefined,
});

const columns = useTable(enums, ({ code, scope }) => {
    switch (code) {
        case 'edit': {
            onEdit(scope.row);
            break;
        }
        case 'remove': {
            onDelete(scope.row);
            break;
        }
        case 'reset': {
            onReset(scope.row);
            break;
        }
        default: {
            break;
        }
    }
});

// 新增&修改
function onEdit(row?: any) {
    editRef.value?.open({
        data: row,
        headers: { 'Content-Type': 'application/json' },
        success: dataTableRef.value?.refresh,
    });
}

// 删除
function onDelete(data: any) {
    const names = Array.isArray(data) ? data.map((item: any) => item.username) : [data.username];
    service.confirm({
        message: `确定对用户名[${names.join(',')}]进行${names.length === 1 ? '删除' : '批量删除'}操作,该操作不可逆?`,
        url: '/system/user',
        data: { usernames: names },
        success: dataTableRef.value?.refresh,
    });
}

// 重置密码
function onReset(row: any) {
    const names = Array.isArray(row) ? row.map((item: any) => item.username) : [row.username];
    service.confirm({
        message: `确定对用户名[${names.join(',')}]进行${names.length === 1 ? '重置' : '批量重置'}密码操作?`,
        url: '/system/user/reset',
        type: 'put',
        data: { usernames: names },
        success({ data }) {
            ElMessageBox.alert(`用户密码重置成功，新密码: ${data}`, '重置密码');
        },
    });
}

onBeforeMount(() => {
    // 获取字典数据
    getDictTrim(['sys_user_sex', 'sys_user_status']).then((rs) => {
        enums.sex = rs[0] ?? [];
        enums.status = rs[1] ?? [];
    });
});
</script>
<template>
    <Page auto-content-height>
        <DataTable ref="dataTableRef" :columns="columns" api="/system/user" selection select-on-row-click>
            <template #extra="{ selections }">
                <el-button v-auth="'system:user:add'" @click="onEdit()" />
                <el-button v-auth="'system:user:remove'" @click="onDelete(selections)" :disabled="selections.length <= 0" type="danger" />
            </template>
            <template #status="{ form }">
                <el-select v-model="form.status" :empty-values="[null, undefined]">
                    <el-option label="全部" value="" />
                    <el-option v-for="(item, index) in enums.status" :key="index" :label="item.label" :value="item.value" />
                </el-select>
            </template>
        </DataTable>

        <Edit ref="editRef" :enums="enums" />
    </Page>
</template>
